import { API_PATHS } from '../../../config/apiConfig';

const API_BASE = '/api/downloadRecord';

export interface DownloadRecord {
  id: number;
  userId: number;
  resourceId: number;
  pointsUsed: number;
  downloadTime: string;
  resourceTitle?: string;
  resourceCover?: string;
}

export interface DownloadRecordListResponse {
  total: number;
  records: DownloadRecord[];
}

export class DownloadRecordService {
  private static getHeaders(includeAuth: boolean = true) {
    const headers: Record<string, string> = {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    };

    if (includeAuth) {
      const token = localStorage.getItem('token');
      if (token) {
        headers['Authorization'] = `Bearer ${token}`;
      }
    }

    return headers;
  }

  // 获取用户下载记录列表
  static async getDownloadRecordList(page: number = 1, size: number = 10): Promise<DownloadRecordListResponse> {
    const response = await fetch(`${API_BASE}/list?page=${page}&size=${size}`, {
      method: 'GET',
      headers: this.getHeaders()
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || '获取下载记录列表失败');
    }

    return response.json();
  }
}